package com.hy.dao.impl;
/*
 * @ Date: 2024/5/14 22:33
 * @ Description: OrdersPartstableDaoImpl
 **/

import com.hy.bean.OrdersPartstable;
import com.hy.dao.OrdersPartstableDao;
import com.hy.dto.OrdersPartstableDto;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class OrdersPartstableDaoImpl implements OrdersPartstableDao {

    private QueryRunner qr = new QueryRunner();

    @Override
    public List<Map<String, Object>> queryById(Integer id) {
        Connection connection = null;
        try {
            connection = DBManager.getConnection();
            String sql = "select op.*,o.orderId,c.partCode,c.partName from orders_partstable op\n" +
                    "left join orderstable o on op.ordersTable_id = o.id \n" +
                    "left join carpartstable c on op.partsTable_id = c.id\n" +
                    "where o.id = ?";
            List<Map<String, Object>> mapList = qr.query(connection,sql,new MapListHandler(),id);
            return mapList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @Override
    public List<OrdersPartstable> queryAllByLimit() {
        return null;
    }

    @Override
    public long count(OrdersPartstable ordersPartstable) {
        return 0;
    }

    @Override
    public boolean insert02(OrdersPartstableDto params, Connection connection) {
        String sql = "insert into orders_partstable(id,ordersTable_id,partsTable_id,orderCount,payCount,price,totalPrice) values(null,?,?,?,?,?,?)";
        try {
            int count = qr.update(connection, sql, params.getOrdersTable_id(),params.getPartsTable_id()
                    ,params.getOrderCount(),0,params.getPrice(),params.getOrderCount()*params.getPrice());
            return count > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(OrdersPartstable params,int id, Connection connection) {
        String sql = "insert into orders_partstable(id,ordersTable_id,partsTable_id,orderCount,payCount,price,totalPrice) values(null,?,?,?,?,?,?)";
        try {
            return qr.update(connection, sql, id,params.getPartstableId()
                    ,params.getOrdercount(),0,params.getPrice()
                    ,params.getOrdercount()*params.getPrice());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(OrdersPartstableDto ordersPartstable, Connection connection) {
        String sql = "update orders_partstable set orderCount = ?,totalPrice = ? where ordersTable_id = ? and id = ?";

        try {
            int count = qr.update(connection, sql, ordersPartstable.getOrderCount(),ordersPartstable.getOrderCount()*ordersPartstable.getPrice(),ordersPartstable.getOrdersTable_id(),ordersPartstable.getId());
            return count > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<OrdersPartstableDto> queryBy(Integer id) {
        Connection connection = null;

        try {
            connection = DBManager.getConnection();
            String sql = "select * from orders_partstable where id = ?";
            return qr.query(connection,sql, new BeanListHandler<>(OrdersPartstableDto.class), id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @Override
    public int updatePayCountById(Connection connection, Integer id, Integer payCount, Integer orderCount){
        String sql = "update orders_partstable set payCount = ?,orderCount = ? where id = ?";
        try {
            return qr.update(connection, sql, payCount,orderCount, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }
}
